//
//  LYBar.m
//  LYTabbarDemo
//
//  Created by 刘宇 on 16/6/23.
//  Copyright © 2016年 liuyu. All rights reserved.
//

#import "LYBar.h"
#import "UIView+Extension.h"
@implementation LYBar

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
- (instancetype)init
{
    self = [super init];
    if (self) {
        UIButton    *centerButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [centerButton addTarget:self action:@selector(centerButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        centerButton.bounds = CGRectMake(0, 0, 50, 50);
        [centerButton setBackgroundImage:[UIImage imageNamed:@"center"] forState:UIControlStateNormal];
        [centerButton setBackgroundImage:[UIImage imageNamed:@"center_selected"] forState:UIControlStateHighlighted];
        _centerButtonOffset = 0.5;
        [self addSubview:centerButton];
        _centerButton = centerButton;
        
        
        //去掉tabbar的顶部线条
        CGRect rect = CGRectMake(0, 0, SCREEN_WIDTH, 49);
        UIGraphicsBeginImageContext(rect.size);
        CGContextRef context = UIGraphicsGetCurrentContext();
        CGContextSetFillColorWithColor(context, [[UIColor clearColor] CGColor]);
        CGContextFillRect(context, rect);
        UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        [self setBackgroundImage:img];
        [self setShadowImage:img];
        
        
        //中间不突出的白色背景
        [self setBackgroundImage:[UIImage imageNamed:@"tabbar2"]];
        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(changeBg:) name:@"CHANGE_BAR_BG" object:nil];
        [self layoutSubviews];
    }
    return self;
}


- (void)layoutSubviews
{
    [super layoutSubviews];
    
    // 1.设置加号按钮的位置
    self.centerButton.centerX = self.width*0.5;
    self.centerButton.centerY = self.height*self.centerButtonOffset;
 
    
    // 2.设置其他tabbarButton的frame
    CGFloat tabBarButtonW = self.width / (CGFloat)(_tabbarButtonCount+1);
    CGFloat tabBarButtonIndex = 0;
    
    for (UIView *child in self.subviews) {
        Class class = NSClassFromString(@"UITabBarButton");
        
        if ([child isKindOfClass:class]) {
            // 设置x

            child.x = tabBarButtonIndex * tabBarButtonW;
            // 设置宽度
            child.width = tabBarButtonW;
            // 增加索引
            tabBarButtonIndex++;
            if (tabBarButtonIndex == _tabbarButtonCount/2) {
                tabBarButtonIndex++;
            }
        }
    }
    
    [self bringSubviewToFront:_centerButton];
}

- (void)centerButtonClick:(UIButton *)sender {
    if (_delegateCustom && [_delegateCustom respondsToSelector:@selector(tabBar:DidClickCenterButton:)]) {
        [_delegateCustom tabBar:self DidClickCenterButton:sender];
    }
}
- (void)dealloc
{
    [[NSNotificationCenter defaultCenter]removeObserver:self name:@"CHANGE_BAR_BG" object:nil];
}

- (void)changeBg:(NSNotification    *)sender {
    NSString    *tag = sender.userInfo[@"tag"];
    if ([tag isEqualToString:@"0"]) {
        
        //中间不突出的白色背景
        [self setBackgroundImage:[UIImage imageNamed:@"tabbar2"]];
    }else{
//                中间突出的白色背景
        [self setBackgroundImage:[UIImage imageNamed:@"tabbar"]];
    }
}

@end

